<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

   	protected function _initDoctype()
    {

	}

    protected function _initAutoload()
    {
        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'Default_',
            'basePath'  => dirname(__FILE__),
        ));
        return $autoloader;
    }
	
	protected function _initAcl()
    {
		$acl = new Zend_Acl();
		Zend_Registry::set('acl',$acl);
		
		$auth = Zend_Auth::getInstance();
		$auth->setStorage(new Zend_Auth_Storage_Session());
    }
    
	protected function _initConfig()
	{
		$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini',APPLICATION_ENV);
		Zend_Registry::set('config',$config);
		
		return $config;
	}
	
	protected function _initLog()
	{
		$writer = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/../data/logs/spagooy.log');				
		$logger = new Zend_Log($writer);

		Zend_Registry::set('errLog',$logger);
		
		return $logger;
	}
	
	protected function _initActionlog()
	{
		$writer = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/../data/logs/spagooyusers.log');				
		$logger = new Zend_Log($writer);
		$logger->addPriority('LOGUSERS', 8);
		Zend_Registry::set('userLog',$logger);
		
		return $logger;
	}

	protected function _initTranslate()
	{
		$locale = new Zend_Locale();		
		$lang;
		$translate;

		$url = $_SERVER['REQUEST_URI'];
		$params = explode('/', $url);
		$languagesString = Zend_Registry::get('config')->languages;
		$languages = explode('/', $languagesString);
		$urlLanguage;
		if (in_array($params[1], $languages))
		{
			$urlLanguage = ($params[1] . "_" . 	Zend_Registry::get('config')->$params[1]);
		}
		
		$defaultLanguage = Zend_Registry::get('config')->defaultLanguage;
		$localeLanguage = $locale->getLanguage() . '_' . $locale->getRegion();
		
		$defaultLanguageFile = APPLICATION_PATH . '/../data/languages/' . $defaultLanguage . '.ini';
		$localeLanguageFile = APPLICATION_PATH . '/../data/languages/' . $localeLanguage . '.ini';
		$urlLanguageFile = APPLICATION_PATH . '/../data/languages/' . $urlLanguage . '.ini';
		
		if (file_exists($urlLanguageFile))
		{
			$lang = $urlLanguage;
		}
/*
 		elseif(file_exists($localeLanguageFile))
		{
			$lang = $localeLanguage;
		} 
*/
		elseif (file_exists($defaultLanguageFile))
		{
			$lang = $defaultLanguage;			
		}
		
		$langFile = APPLICATION_PATH . '/../data/languages/' . $lang . '.ini';
		
		if($lang != NULL)
		{			
			$translate = new Zend_Translate('ini',$langFile, substr($lang,0,2));			
			Zend_Registry::set('translate',$translate);	
		}
		else
		{
			Zend_Registry::get('errLog')->err("Kunde inte hitta språkfilen \"$langFile\"");
		}
		return $translate;
	}
	
	protected function _initController()
	{
	//	require_once 'Zend/Controller/Action/HelperBroker.php';
	//	Zend_Controller_Action_HelperBroker::addPrefix('ZendY_JQuery_Controller_Action_Helper');
	}
	
	protected function _initView()
	{		
		$view = new Zend_View();
		$view->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper');
		
		$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
		$viewRenderer->setView($view);
		Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
		
		$view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
		
//        $this->bootstrap('view');
//        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
	}
/*	
	protected function _initDb()
	{
		$this->getPluginResource('db')->getDbAdapter()->query('SET NAMES UTF8;');
		Zend_Registry::set('db', $this->getPluginResource('db')->getDbAdapter());
	}
*/
}
